// pages/home/details/details.js
var timeInfo = require("../../data/data.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderInfo:{},
    dateList:[],
    question:"",
    timeList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    debugger
    let order = JSON.parse(options.order)
      this.setData({
        orderInfo:order,
        timeList: timeInfo.timeList,
      })
      this.getDate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  bindcustomPickerChangeCar(e) {
    const eValue = e.detail
    this.setData({
      car: eValue,
    })
  },
   //custom picker
   showCustomPicker(e){
    // 组件间通信，父组件可以通过this.selectComponent方法获取子组件实例对象，这样就可以直接访问组件的任意数据和方法。
    const customPicker = this.selectComponent(`#custom-picker-${e.currentTarget.dataset.id}`) // 获得子组件实例对象
    
    customPicker.showPicker()
  },

  bindcustomPickerChangeDate(e) {
    if (this.data.flag != 0) {
      const eValue = e.detail
      const date = this.data.dateList[eValue];
      this.setData({
        date : eValue,
        dateDel: date.day,
        orderdatedata: date.day
       })
    } else {
      const eValue = e.detail
      const date = this.data.dateList[eValue];
      this.setData({
        date : eValue,
        dateDel: date.day
      })
    }
  },

  getDate: function(){
    var now = new Date();
    var currentTime = now.getHours();

    var month = now.getMonth() + 1
    var year = now.getFullYear()
    var day = now.getDate()

    let dateArr = [];
    for(var i=0; i<14; i++){
      var now = new Date();
      if(currentTime >= 18){
        var end = now.setDate(now.getDate() + (i+1));
      }
      else{
        var end = now.setDate(now.getDate() + i);
      }
      
      var end_day = new Date(end);
      var end_yy = end_day.getFullYear();
      var end_mm = end_day.getMonth() + 1;
      var end_dd = end_day.getDate();
      var endDate = end_yy + "-" + end_mm + "-" + end_dd;
      let end_date = {
        "id":i,
        "day":endDate
      }
      dateArr.push(end_date);
    }
    if (currentTime >= 18){
      currentTime = 0;
    }
    else if (currentTime <= 8){
      currentTime = 0;
    }
    else {
      currentTime -= 7;
    }
    while(this.data.timeList[currentTime].disabled){
      currentTime++
    }
    this.setData ({
      dateList: dateArr,
      dateDel: dateArr[0].day,
      time: currentTime,
      time1: this.data.timeList[currentTime].timeValue
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  bindinputblur: function(e){
    this.data.question = e.detail.value
  },
  click: function(){
    // console.log(this.data.orderInfo.progress)
    let ind = this.data.orderInfo.progress
    if(ind==4){
      // console.log(this.data.question)
      if(this.data.question == ""){
        
        wx.showToast({
          title: "请填写备注",
          icon: "none",
        });
        return;
      }
    }
    this.setData({
      "orderInfo.progress":ind+1
    })
  },
  bindcustomPickerChangeDateTime(e) {
    if (this.data.flag != 0) {
      const eValue = e.detail
      const time = this.data.timeList[eValue]
      this.setData({
          time1: time.timeValue,
          time: eValue,
          ordertimedata: time.timeValue
        })
    } else {
      const eValue = e.detail
      const time = this.data.timeList[eValue]
      this.setData({
          time1: time.timeValue,
          time: eValue
      })
    }
  },
  updateForm: function(e) {
    //更新表单信息
    const orderID = this.data.orderInfo.orderID;
    const ques = this.data.Newquestion;
    const time1 = this.data.time1;
    const dateDel = this.data.dateDel;
    const app = getApp()
    const url = app.globalData.url;
    if (ques == null) {
        wx.navigateBack({
          url:"./../home.js"
        })
    } 
     wx.request({
       url: url + 'form/v2/updateFormDetail',
       method: 'POST',
       data: {
         orderID: orderID,
         question: ques,
         newTime: time1,
         newDate: dateDel
       },
       success: res => {
          wx.navigateBack({
            url: "./../home.js",
            success: res => {
              wx.showToast({
                title: '保存成功',
                icon: 'success'
              })
            }
          })
       },
       fail: res => {

       }
     })
  },
  confirmDoor:  function(e) {
    //已上门，状态改为维护中
    console.log(e);
    const order = e.currentTarget.dataset.order.orderID;
    const app = getApp();
    const url = app.globalData.url;
    wx.request({
      url: url + "form/v1/updateByMaster/Form",
      method: 'POST',
      data: {
        orderID:order,
        status: 4,
      },
      success: res => {
        if (res.data.code == 200) {
            wx.navigateBack({
              url: "../home.js",
              success: res => {
                wx.showToast({
                  title: '订单更新成功',
                })
              }
            })
        }
      },
      fail: res => {
          console.error("上门状态更新失败")
      }
    })
  },
  finishForm: function(e) {
    //结束订单信息
    console.log(e);
    const order = e.currentTarget.dataset.order.orderID;
    const app = getApp();
    const url = app.globalData.url;
    wx.request({
      url: url + "form/v1/updateByMaster/Form",
      method: 'POST',
      data: {
        orderID:order,
        status: 5,
      },
      success: res => {
        if (res.data.code == 200) {
          wx.navigateBack({
            url: "../home.js",
            success: res => {
              wx.showToast({
                title: '订单更新成功',
              })
            }
          })
        }
      },
      fail: res => {
          console.error("完成状态更新失败")
      }
    })
  },
  updateQuestion: function(e) {
    const question = e.detail.value;
    this.setData({
      // orderInfo: {
        Newquestion: question
      // }
    })
  }
})